const App = {
  data() {
    return {
      currentTreeNodeId: "", // 新增：存储当前选中的树节点ID
      Total: "",
      currentPage: 1, //当前页码
      pageSize: 10, // 每页记录数
      input: "", //输入框
      treeData: [], //树的数据
      originalTreeData: [], //原始树的数据
      originalTableData: [], // 原始表格的数据
      columns: [], //表格的列
      loading: false, //表数据的懒加载
      showDialog: false, //设置字段弹窗
      checkedFields: [], //选中的字段
      isDataReady: false,
      Tabletdata: [], //表格的数据
      fields: [], //字段列表
      value: "",
      addTitle:[],
      pageTitle:[],
      pageTime:[],
      active:"1",
      src: ''
      
    };
  },
  mounted() {
    this.Treedata();
    this.getList();
    //获取申请人姓名
    let activityType = sessionStorage.getItem("currentName");
    let time = sessionStorage.getItem("time")
    console.log("申请人姓名", activityType);
    console.log(time);
    
    // 新增：将获取到的类型设置到数据模型
    if(activityType) {
      this.pageTitle = activityType
      this.pageTime = time
    }

    const activeTab = localStorage.getItem('activeTab');
    console.log(activeTab);
    
    if (activeTab) {
      this.active = parseInt(activeTab);
      localStorage.removeItem('activeTab'); // 清除存储
      console.log(this.active);
      
    }
  },
  computed: {
    // 计算属性，返回选中的字段
    isAllChecked() {
      return this.checkedFields.length === this.fields.length;
    },
  },
  methods: {

    handleData(type){
      //点击按钮获取activist
      sessionStorage.setItem("activist", type)
      console.log("activist:",type);
      setTimeout(() => {
        window.location.href="./determinedate_add.html"
      }, 100); // 延迟 100ms 确保数据写入
      
    },
    



    //树的数据 查询树的接口
    Treedata() {
      const _this = this;
      $.ajax({
        url: "https://service.essp.top/apiForMoHe/generalBusiness/getPartyOrganizationTree",
        method: "get",
        success: function (data) {
          console.log(data);
          //赋值到树上
          _this.treeData = data;
          // 使用深拷贝赋值 originalTreeData
          _this.originalTreeData = JSON.parse(JSON.stringify(data));
        },
        dataType: "json",
      });
    },
    //获取表格的列
    getList() {
      const _this = this;
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/generalBusiness/generalQueryAllColumnByTableName`,
        method: "get",
        data: {
          Data: "party_member_application",
        },
        success: (res) => {
          console.log(res);
          sessionStorage.setItem("TABLE_NAME", res[0].TABLE_NAME);
          let PRI = res.filter((item) => {
            return item.COLUMN_KEY === "PRI";
          });
          if (res.length > 0) {
            let key = PRI[0].COLUMN_NAME;
            console.log(key);
            sessionStorage.setItem("key", key);
          }
          let data = res.filter((item) => {
            return item.COLUMN_COMMENT;
          });
          console.log(data);

          let Rdata = data.filter((item) => item.COLUMN_NAME !== "Row_Status");

          let Rdata2 = Rdata.filter(
            (item) => item.COLUMN_NAME !== "Operation_ID"
          );
          let Rdata3 = Rdata2.filter(
            (item) => item.COLUMN_NAME !== "Operation_Datetime"
          );
          console.log(Rdata3);
          _this.columns = Rdata3;
          _this.checkedFields = Rdata3;
          _this.fields = Rdata3;
          _this.isDataReady.value = true;
          _this.loading = true;
          _this.postgetList(_this.currentPage);
        },
        error: (err) => {
          console.log(err);
        },
      });
    },
    //获取表格的数据
    postgetList(data) {
      console.log(data);
      let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
      console.log(TABLE_NAME);
      const _this = this;

      // 构建查询条件
      let condition = "Row_Status='0'";
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/generalBusiness/generalPaginateQueryByTableName`,
        method: "post",
        data: {
          Data: TABLE_NAME,
          Condition: condition,
          PageNumber: this.currentPage, //页码
          RowAmount: 10,
        },
        success: (res) => {
          console.log(111);
          console.log(res);
          _this.Total = res.Total
          _this.loading = false;
          _this.Tabletdata = res.Rows;
          _this.originalTableData = res.Rows; // 保存原始数据
        },
        error: (err) => {
          console.log(err);
        },
      });
    },

    // //添加按钮
    // personalAdd(activityType) {
    //   console.log('点击的字段:', activityType);
    //   let a = "Add";
    //   sessionStorage.setItem("type", a);
    //   // 把activityType存起来
    //   sessionStorage.setItem("addType", activityType);
    //   window.location.href = "./Add.html";
    // },

    //点击事件
    handleData(type){
      console.log(type);
      sessionStorage.setItem("addType", type)
      window.location.href="./determinedate_add.html"
      
    },

    // 点击填写基础信息事件
    handleFill(stepTitle){
      // 获取 <el-step> 的 title 属性
      console.log(stepTitle); // 输出: "基本信息"
      sessionStorage.setItem("addTitle", stepTitle)
      window.location.href="./basic_add.html"
    },
    //编辑基础信息按钮
    handleEdit(data) {
      console.log("原始data:", data);
      let a = "Edit";
      sessionStorage.setItem("type", a);
      sessionStorage.setItem("data",data);
        // 2. 验证存储结果（可选）
      console.log("存储的data:", sessionStorage.getItem("data"));
      //获取觉组活动参与人员流水号存起来
      // sessionStorage.setItem("rowkey", row[key]);
      // 3. 确保存储完成后再跳转
      setTimeout(() => {
        window.location.href = "./basic_add.html";
      }, 50); // 微小延迟确保存储完成
    },
    // 弹窗全选
    handleAllChecked(val) {
      console.log(val);
      console.log(val);
      let a; // 提前声明变量 a
      if (val === false) {
        console.log(this.fields);
        a = this.fields.filter((item) => {
          return item.COLUMN_KEY == "PRI";
        });
        console.log(a);
      } else {
        a = [...this.fields]; // 当 val 为 true 时，给 a 赋值为 props.fields 的副本
      }
      // 使用 emit 更新 checkedFields
      this.checkedFields = a;
    },
    saveFields() {
      const selectedFields = this.fields.filter((item) => {
        return this.checkedFields.some(
          (cItem) => cItem && cItem.COLUMN_NAME === item.COLUMN_NAME
        );
      });
      console.log("选中的字段:", selectedFields);
      // 更新 columns
      this.columns = selectedFields;
      this.showDialog = false;
    },
    //删除数据
    handleDelete(index, row) {
      let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
      console.log(TABLE_NAME);
      console.log(index, row);
      row.Row_Status = "1";
      let lArray = new Array();
      lArray.push(row);
      let lJSONObject = new Object();
      lJSONObject[TABLE_NAME] = lArray; //换表
      let lJSONObject_DataSet = new Object(); // Modify
      lJSONObject_DataSet.Modify = lJSONObject; //换表
      const _this = this;
      // return;
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/clientOperation/generalSaveForClient`,
        method: "post",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: (res) => {
          console.log(res);
          _this.postgetList(_this.currentPage);
        },
        error: (err) => {
          console.log(err);
        },
      });
    },
    //树的点击事件
    handleNodeClick(data) {
      console.log(data);
      console.log(data.Party_Organization_SerialNumber)
      //把获取到得党组织编号传到postgetList方法里
      this.currentTreeNodeId = data.Party_Organization_SerialNumber; // 存储当前选中的节点ID
      console.log(this.currentTreeNodeId);
      
      this.currentPage = 1; // 重置到第一页
      this.postgetList(this.currentTreeNodeId); // 调用查询方法
    },
  },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus,{locale: zhCn});
app.mount("#app");
